package com.mashibing.servicemap.remote;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.mashibing.constant.AmapConfigConstant;
import org.mashibing.response.DirectionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author:
 * @create: 2024/4/3 15:23
 * @description:
 */
@Component
@Slf4j
public class MapDirectionClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 远程调用高德地图路径规划API
     */
    public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {

        // 拼接url
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstant.DIRECTION_URL);
        urlBuilder.append("?origin=").append(depLongitude).append(",").append(depLatitude);
        urlBuilder.append("&destination=").append(destLongitude).append(",").append(destLatitude);
        urlBuilder.append("&output=json").append("&key=").append(amapKey);

        log.info("url : {}", urlBuilder);
        // 调用高德路径规划服务
        ResponseEntity<String> forEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);
        String body = forEntity.getBody();
        // 处理返回结果
        return parseDirectionEntity(body);
    }

    /**
     * 解析路径规划返回的json数据
     */
    private DirectionResponse parseDirectionEntity(String body) {
        DirectionResponse directionResponse = null;

        JSONObject jsonObject = JSONObject.parseObject(body);
        try {
            if (jsonObject.containsKey(AmapConfigConstant.STATUS)) {
                Integer status = jsonObject.getInteger(AmapConfigConstant.STATUS);
                if (status == 1) {
                    if (jsonObject.containsKey(AmapConfigConstant.ROUTE)) {
                        JSONObject route = jsonObject.getJSONObject(AmapConfigConstant.ROUTE);
                        if (route.containsKey(AmapConfigConstant.PATHS)) {
                            JSONArray paths = route.getJSONArray(AmapConfigConstant.PATHS);
                            JSONObject pathsJSONObject = paths.getJSONObject(0);
                            directionResponse = new DirectionResponse();
                            // 距离
                            if (pathsJSONObject.containsKey(AmapConfigConstant.DISTANCE)) {
                                Integer distance = pathsJSONObject.getInteger(AmapConfigConstant.DISTANCE);
                                directionResponse.setDistance(distance);
                            }
                            // 时间
                            if (pathsJSONObject.containsKey(AmapConfigConstant.DURATION)) {
                                Integer duration = pathsJSONObject.getInteger(AmapConfigConstant.DURATION);
                                directionResponse.setDuration(duration);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return directionResponse;
    }
}
